<?php
/**
 * This file is part of the UXLabs project.
 *
 * (c) UXLabs project <http://code.uxlabs.me>
 *
 */

namespace uxl\app\api\components;

use Yii;
use yii\base\Object;
use yii\helpers\Json;

/**
 * 接口请求参数
 *
 * @property string $accessKey 访问所需的密钥，参数名 access_key
 * @property string $userAuthKey 登录用户授权密钥，参数名 user_auth_key
 * @property int $timestamp 请求参数，时间戳，参数名 timestamp
 * @property string $once 请求附带的随机字符串,保证每次请求都不一样,参数名 once
 * @property string $attach 请求附带参数,API服务端不做任何处理,原值返回,参数名 attach
 * @property string $format 请求数据格式和返回数据格式,默认为JSON,可选择: JSON/XML/JSONP,参数名 format
 * @property string $signType 签名方法,默认MD5,参数名 sign_type
 * @property string $sign 签名字符串,参数名 sign
 * @property string $version 请求的接口的版本, 参数名 version
 * @property string $method 请求接口的方法名, 参数名 method
 * @property string $data 请求的业务数据,参数名 data
 *
 * @package uxl\app\api\components
 * @author ice deng <ice.deng@qq.com>
 */
class Parameters extends Object
{
    protected $_data = [];

    protected $urlMap = [
        'access_key' => 'accessKey',
        'user_auth_key' => 'userAuthKey',
        'timestamp' => 'timestamp',
        'once' => 'once',
        'attach' => 'attach',
        'format' => 'format',
        'sign_type' => 'signType',
        'sign' => 'sign',
        'version' => 'version',
        'method' => 'method',
        'data' => 'data'
    ];

    /**
     * Returns the value of an object property.
     *
     * Do not call this method directly as it is a PHP magic method that
     * will be implicitly called when executing `$value = $object->property;`.
     * @param string $name the property name
     * @return mixed the property value
     * @see __set()
     */
    public function __get($name)
    {
        $getter = 'get' . $name;
        if (method_exists($this, $getter)) {
            return $this->$getter();
        } elseif (isset($this->_data[$name])) {
            return $this->_data[$name];
        } else {
            return null;
        }
    }

    /**
     * Sets value of an object property.
     *
     * Do not call this method directly as it is a PHP magic method that
     * will be implicitly called when executing `$object->property = $value;`.
     * @param string $name the property name or the event name
     * @param mixed $value the property value
     * @see __get()
     */
    public function __set($name, $value)
    {
        $setter = 'set' . $name;
        if (method_exists($this, $setter)) {
            $this->$setter($value);
        } else {
            $this->_data[$name] = $value;
        }
    }

    public function init()
    {
        // 获取URL参数
        $req = Yii::$app->getRequest();
        foreach ($this->urlMap as $k => $v) {
            $d = $req->post($k, null);
            if ($d !== null) {
                $this->_data[$v] = $d;
            }
        }
    }

    /**
     * 获取json格式的参数
     * @return string
     */
    public function toJson()
    {
        return Json::encode($this->_data);
    }

    /**
     * 获取数组格式的参数
     * @return string
     */
    public function toArray()
    {
        return $this->_data;
    }

    /**
     * 获取业务参数解析后的值
     * @return null|array
     */
    public function getDataValue()
    {
        if (isset($this->_data['data']) && empty($this->_data['data'])) {
            return Json::decode($this->_data['data'], true);
        }
        return null;
    }
}